package com.cango.qutz.web.auth.authdept.service.impl;

import java.util.Date;
import java.util.List;

import com.cango.qutz.web.auth.authdept.service.IAuthDeptService;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.qutz.web.auth.authdept.dao.IAuthDeptDao;
import com.cango.qutz.web.auth.authdept.service.IAuthDeptService;
import com.cango.qutz.web.entity.AuthDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cango.qutz.web.auth.authdept.dao.IAuthDeptDao;
import com.cango.qutz.web.entity.AuthDeptEntity;
import com.cango.framework.base.pagination.PaginationResult;

/**
 * <p>ClassName: AuthDeptServiceImpl</p>
 * <p>Description: 部门维护Service实现类</p>
 * <p>Author: wzx</p>
 * <p>Date: 2015-04-20</p>
 */
@Service("authDeptService")
public class AuthDeptServiceImpl implements IAuthDeptService {

    /** 部门sql的命名空间 */
    private static final String SQL_NAMESPACE = "authDept.";

    /** 新增部门的sql id */
    private static final String SQL_INSERT = "insertAuthDept";

    /** 更新部门的sql id */
    private static final String SQL_UPDATE = "updateAuthDept";

    /** 删除部门的sql id */
    private static final String SQL_DELETE = "deleteAuthDept";

    /** 根据主键查询部门的sql id */
    private static final String SQL_QUERY_BY_ID = "queryAuthDeptById";

    /** 分页查询部门的sql id */
    private static final String SQL_QUERY_BY_PAGE = "queryAuthDeptsByPage";

    /** 根据部门代码查询已有的数量 id */
    private static final String SQL_QUERY_COUNT_BY_CODE = "queryCountByCode";

    /** 不分页查询所有有效部门 */
    private static final String SQL_QUERY_FOR_LIST = "queryAuthDepts";

    /**
     * <p>
     * Field authDeptDao: 部门DAO注入
     * </p>
     */
    @Autowired
    private IAuthDeptDao authDeptDao;


    @Override
    public int insert(AuthDeptEntity authDept) {
        authDept.setIsDel(0);
        int id = this.authDeptDao.insert(SQL_NAMESPACE + SQL_INSERT, authDept);
        return id;
    }

    @Override
    public int update(AuthDeptEntity authDept) {
        AuthDeptEntity eidtAuthDept = this.queryObjectById(authDept.getDeptId());
        eidtAuthDept.setUpdateUser(authDept.getUpdateUser());
        eidtAuthDept.setUpdateTime(new Date());
        eidtAuthDept.setDeptName(authDept.getDeptName());
        //目前不让改部门代码
//        eidtAuthDept.setDeptCode(authDept.getDeptCode());
        eidtAuthDept.setRemark(authDept.getRemark());
        return this.authDeptDao.update(SQL_NAMESPACE + SQL_UPDATE, eidtAuthDept);
    }

    @Override
    public int delete(AuthDeptEntity deleteAuthDept) {
        deleteAuthDept.setUpdateTime(new Date());
        return this.authDeptDao.delete(SQL_NAMESPACE + SQL_DELETE, deleteAuthDept);
    }

    @Override
    public PaginationResult<AuthDeptEntity> queryListByPage(
                                            AuthDeptEntity param, PageInfo pageinfo) {
        return this.authDeptDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_BY_PAGE, param, pageinfo);
    }

    @Override
    public boolean validDeptCode(String deptCode) {
        int count = this.authDeptDao.queryCountByCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_CODE, deptCode);
        return count <= 0;
    }

    @Override
    public AuthDeptEntity queryObjectById(Integer deptId) {
        return this.authDeptDao.queryObjectById(SQL_NAMESPACE + SQL_QUERY_BY_ID, deptId);
    }

    @Override
    public List<AuthDeptEntity> queryList(AuthDeptEntity param) {
        return this.authDeptDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST, param);
    }
}
